package it.blueocean.acanto.taglibrary.jsp.taglib.tags.datatable;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.DataSet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataTableRowTag extends BodyTagSupport
{
	private static final long serialVersionUID = 206169729988024336L;

	private final static Logger logger_ = LoggerFactory.getLogger(DataTableRowTag.class);

	private Object[] rows_;

	private int currentRow_;

	private String datasetNameExpr_;

	private String rowStyleExpr_;

	private String evenRowStyleExpr_;

	private String oddRowStyleExpr_;

	private String selectedRowStyleExpr_;

	private String rowClassExpr_;

	private String evenRowClassExpr_;

	private String oddRowClassExpr_;

	private String selectedRowClassExpr_;

	private String omitTrTagExpr_;

	private DataSet dataset_;

	private String evenRowStyle_;

	private String oddRowStyle_;

	private String selectedRowStyle_;

	private String evenRowClass_;

	private String oddRowClass_;

	private String selectedRowClass_;

	private boolean omitTrTag_;

	private String onclick = null;

	public int doStartTag() throws JspException
	{
		evaluateExpressions();
		this.rows_ = this.dataset_.getRows();
		this.currentRow_ = 0;
		if(this.rows_.length > 0)
		{
			try
			{
				if(!(this.omitTrTag_))
					printOpenedTag();
			}
			catch (IOException localIOException)
			{
				logger_.error(localIOException.getMessage());
				return 0;
			}
			HashMap<String, Object> localHashMap = new HashMap<String, Object>();
			if(this.pageContext.getAttribute("row") != null)
			{
				localHashMap.put("row", this.pageContext.getAttribute("row"));
				this.pageContext.removeAttribute("row");
			}
			if(this.pageContext.getAttribute("rowNumber") != null)
			{
				localHashMap.put("rowNumber", this.pageContext.getAttribute("rowNumber"));
				this.pageContext.removeAttribute("rowNumber");
			}
			if(this.pageContext.getAttribute("absoluteRowNumber") != null)
			{
				localHashMap.put("absoluteRowNumber", this.pageContext.getAttribute("absoluteRowNumber"));
				this.pageContext.removeAttribute("absoluteRowNumber");
			}
			if(this.pageContext.getAttribute("selected") != null)
			{
				localHashMap.put("selected", this.pageContext.getAttribute("selected"));
				this.pageContext.removeAttribute("selected");
			}
			if(this.pageContext.getAttribute("outer") != null)
			{
				localHashMap.put("outer", this.pageContext.getAttribute("outer"));
				this.pageContext.removeAttribute("outer");
			}
			this.pageContext.setAttribute("outer", localHashMap);
			this.pageContext.setAttribute("row", this.rows_[this.currentRow_]);
			this.pageContext.setAttribute("rowNumber", new Integer(this.currentRow_ + 1));
			this.pageContext.setAttribute("absoluteRowNumber", new Long(this.dataset_.getFirstRowNumber()
					+ this.currentRow_));
			this.pageContext.setAttribute("selected",
					new Boolean(this.dataset_.getCurrentRow() == this.dataset_.getFirstRowNumber() + this.currentRow_));
			return 1;
		}
		return 0;
	}

	public int doAfterBody() throws JspException
	{
		try
		{
			if(!(this.omitTrTag_))
				this.pageContext.getOut().print("</tr>");
			this.currentRow_ += 1;
			if(this.currentRow_ < this.rows_.length)
			{
				if(!(this.omitTrTag_))
					printOpenedTag();
				this.pageContext.setAttribute("row", this.rows_[this.currentRow_]);
				this.pageContext.setAttribute("rowNumber", new Integer(this.currentRow_ + 1));
				this.pageContext.setAttribute("absoluteRowNumber", new Long(this.dataset_.getFirstRowNumber()
						+ this.currentRow_));
				this.pageContext.setAttribute("selected",
						new Boolean(this.dataset_.getCurrentRow() == this.dataset_.getFirstRowNumber()
								+ this.currentRow_));
				return 2;
			}
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		this.pageContext.removeAttribute("row");
		this.pageContext.removeAttribute("rowNumber");
		this.pageContext.removeAttribute("absoluteRowNumber");
		this.pageContext.removeAttribute("selected");
		Map<?, ?> localMap = (Map<?, ?>) this.pageContext.getAttribute("outer");
		this.pageContext.removeAttribute("outer");
		if(localMap != null)
		{
			if(localMap.get("row") != null)
				this.pageContext.setAttribute("row", localMap.get("row"));
			if(localMap.get("rowNumber") != null)
				this.pageContext.setAttribute("rowNumber", localMap.get("rowNumber"));
			if(localMap.get("absoluteRowNumber") != null)
				this.pageContext.setAttribute("absoluteRowNumber", localMap.get("absoluteRowNumber"));
			if(localMap.get("selected") != null)
				this.pageContext.setAttribute("selected", localMap.get("selected"));
			if(localMap.get("outer") != null)
				this.pageContext.setAttribute("outer", localMap.get("outer"));
		}
		return 6;
	}

	private void printOpenedTag() throws IOException
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		String str1;
		String str2;
		if((this.currentRow_ + 1) % 2 == 0)
		{
			str1 = this.evenRowClass_;
			str2 = this.evenRowStyle_;
		}
		else
		{
			str1 = this.oddRowClass_;
			str2 = this.oddRowStyle_;
		}
		if(this.currentRow_ + this.dataset_.getFirstRowNumber() == this.dataset_.getCurrentRow())
		{
			if(this.selectedRowClass_ != null)
				str1 = this.selectedRowClass_;
			if(this.selectedRowStyle_ != null)
				str2 = this.selectedRowStyle_;
		}
		localJspWriter.write("<tr"
				+ (((str1 != null) && (!(str1.trim().equals("")))) ? " class=\"" + str1 + "\" " : "")
				+ (((str2 != null) && (!(str2.trim().equals("")))) ? " style=\"" + str2 + "\" " : "")
				+ (((this.onclick != null) && (!(this.onclick.trim().equals("")))) ? " onclick=\"" + this.onclick
						+ "\" " : "") + ">");
	}

	private void evaluateExpressions() throws JspException
	{
		DataTableTag localDataTableTag = (DataTableTag) findAncestorWithClass(this, DataTableTag.class);
		if(this.datasetNameExpr_ != null)
		{
			String str = (String) ExpressionEvaluatorManager.evaluate("dataset", this.datasetNameExpr_, String.class,
					this, this.pageContext);
			this.dataset_ = ((DataSet) this.pageContext.getAttribute(str));
		}
		else if(localDataTableTag != null)
		{
			this.dataset_ = localDataTableTag.getDataset();
		}
		else
		{
			throw new JspException(
					"Cannot find an appropriate dataset, tag datarow must be inside a datatable tag or a dataset name must be specified");
		}
		if(this.rowClassExpr_ != null)
		{
			this.evenRowClass_ = ((String) ExpressionEvaluatorManager.evaluate("class", this.rowClassExpr_,
					String.class, this, this.pageContext));
			this.oddRowClass_ = this.evenRowClass_;
		}
		else
		{
			if(this.evenRowClassExpr_ != null)
				this.evenRowClass_ = ((String) ExpressionEvaluatorManager.evaluate("evenRowClass",
						this.evenRowClassExpr_, String.class, this, this.pageContext));
			else
				this.evenRowClass_ = null;
			if(this.oddRowClassExpr_ != null)
				this.oddRowClass_ = ((String) ExpressionEvaluatorManager.evaluate("oddRowClass", this.oddRowClassExpr_,
						String.class, this, this.pageContext));
			else
				this.oddRowClass_ = null;
		}
		if(this.rowStyleExpr_ != null)
		{
			this.evenRowStyle_ = ((String) ExpressionEvaluatorManager.evaluate("style", this.rowStyleExpr_,
					String.class, this, this.pageContext));
			this.oddRowStyle_ = this.evenRowStyle_;
		}
		else
		{
			if(this.evenRowStyleExpr_ != null)
				this.evenRowStyle_ = ((String) ExpressionEvaluatorManager.evaluate("evenRowStyle",
						this.evenRowStyleExpr_, String.class, this, this.pageContext));
			else
				this.evenRowStyle_ = null;
			if(this.oddRowStyleExpr_ != null)
				this.oddRowStyle_ = ((String) ExpressionEvaluatorManager.evaluate("oddRowStyle", this.oddRowStyleExpr_,
						String.class, this, this.pageContext));
			else
				this.oddRowStyle_ = null;
		}
		if(this.selectedRowStyleExpr_ != null)
			this.selectedRowStyle_ = ((String) ExpressionEvaluatorManager.evaluate("selectedRowStyle",
					this.selectedRowStyleExpr_, String.class, this, this.pageContext));
		else
			this.selectedRowStyle_ = null;
		if(this.selectedRowClassExpr_ != null)
			this.selectedRowClass_ = ((String) ExpressionEvaluatorManager.evaluate("selectedRowClass",
					this.selectedRowClassExpr_, String.class, this, this.pageContext));
		else
			this.selectedRowClass_ = null;
		if(this.omitTrTagExpr_ != null)
			this.omitTrTag_ = ((Boolean) ExpressionEvaluatorManager.evaluate("omitTrTag", this.omitTrTagExpr_,
					Boolean.class, this, this.pageContext)).booleanValue();
		else
			this.omitTrTag_ = false;
	}

	public void setEvenRowClass(String paramString)
	{
		this.evenRowClassExpr_ = paramString;
	}

	public void setEvenRowStyle(String paramString)
	{
		this.evenRowStyleExpr_ = paramString;
	}

	public void setOddRowClass(String paramString)
	{
		this.oddRowClassExpr_ = paramString;
	}

	public void setOddRowStyle(String paramString)
	{
		this.oddRowStyleExpr_ = paramString;
	}

	public void setSelRowClass(String paramString)
	{
		this.selectedRowClassExpr_ = paramString;
	}

	public void setSelRowStyle(String paramString)
	{
		this.selectedRowStyleExpr_ = paramString;
	}

	public void setHtmlClass(String paramString)
	{
		this.rowClassExpr_ = paramString;
	}

	public void setStyle(String paramString)
	{
		this.rowStyleExpr_ = paramString;
	}

	public void setOmitTrTag(String paramString)
	{
		this.omitTrTagExpr_ = paramString;
	}

	public void setDataset(String paramString)
	{
		this.datasetNameExpr_ = paramString;
	}

	public String getOnclick()
	{
		return this.onclick;
	}

	public void setOnclick(String paramString)
	{
		this.onclick = paramString;
	}

}
